package com.yw.thesimpllestplayer.mediaplayer.decoder;

import android.media.MediaCodec;
import android.media.MediaFormat;

import com.yw.thesimpllestplayer.audioplayer.AudioPlayer;
import com.yw.thesimpllestplayer.extractor.AudioExtractor;
import com.yw.thesimpllestplayer.extractor.IExtractor;

import java.nio.ByteBuffer;

/**
 * @ProjectName: TheSimpllestplayer
 * @Package: com.yw.thesimpllestplayer.mediaplayer.decoder
 * @ClassName: AudioDecoder
 * @Description: 音频解码器
 * @Author: wei.yang
 * @CreateDate: 2021/11/6 13:55
 * @UpdateUser: 更新者：wei.yang
 * @UpdateDate: 2021/11/6 13:55
 * @UpdateRemark: 更新说明：
 * @Version: 1.0
 */
public class AudioDecoder extends BaseDecoder {
    private AudioPlayer audioPlayer;

    public AudioDecoder(String filePath) {
        super(filePath);
    }

    @Override
    public boolean check() {
        return true;
    }

    @Override
    public IExtractor initExtractor(String filePath) {
        return new AudioExtractor(filePath);
    }

    @Override
    public void initSpecParams(MediaFormat format) {
        if (audioPlayer == null) {
            audioPlayer = new AudioPlayer(format);
        }

    }

    @Override
    public boolean configCodec(MediaCodec codec, MediaFormat format) {
        codec.configure(format, null, null, 0);
        return true;
    }

    @Override
    public boolean initRender() {
        audioPlayer.initPlayer();
        return true;
    }

    @Override
    public void render(ByteBuffer outputBuffer, MediaCodec.BufferInfo bufferInfo) {
        audioPlayer.play(outputBuffer, bufferInfo);
    }

    @Override
    public void finishDecode() {
        audioPlayer.stop();
        audioPlayer.release();

    }
}
